package com.five.welcome.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.five.welcome.constant.costConstant;
import com.five.welcome.dao.OrdersMapper;
import com.five.welcome.entity.Orders;
import com.five.welcome.entity.statistic.PayStatusCount;
import com.five.welcome.service.OrdersService;
import com.five.welcome.utils.OrderIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ywt
 * @since 2024-07-12
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    //通过学号查询订单
    @Override
    public Orders getOrderBySNumber(String sNumber) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("s_number", sNumber);
        return ordersMapper.selectOne(queryWrapper);
    }

    //查询所有订单
    @Override
    public List<Orders> selectAll() {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*");
        return ordersMapper.selectList(queryWrapper);
    }

    //通过订单号查询订单
    @Override
    public Orders getOrderByOrderId(String orderId) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return ordersMapper.selectOne(queryWrapper);
    }

    //修改订单
    @Override
    public void updateByOrderId(Orders orders) {
        ordersMapper.updateByOrderId(orders);
    }

    //订单分页条件查询
    @Override
    public Page<Orders> pageSelectAll(Integer pageNumber, Integer pageSize, String sNumber, String payStatus) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(sNumber != null, "s_number", sNumber)
                .eq(payStatus != null, "pay_status", payStatus);
        return page(Page.of(pageNumber, pageSize), queryWrapper);
    }

    @Override
    public Orders getIsOrderBySNumber(String sNumber) {
        Orders isOrder = getOrderBySNumber(sNumber);
        if (isOrder != null) {
            return isOrder;
        }
        //创建订单
        Orders newOrder = new Orders();
        newOrder.setSNumber(sNumber);
        newOrder.setOrderId(OrderIdUtils.generateOrderNumber());
        newOrder.setLiveCost(costConstant.LIVECOST);
        newOrder.setStudyCost(costConstant.STUDYCOST);
        newOrder.setOtherCost(costConstant.OTHERCOST);
        newOrder.setTotal(costConstant.TOTAL);
        ordersMapper.insert(newOrder);
        return getOrderBySNumber(sNumber);
    }

    @Override
    public List<PayStatusCount> PayStatusCount() {
       return ordersMapper.PayStatusCount();
    }
}
